﻿using System;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    public interface ISaveUtility : IUtility
    {
        void SaveInt(string key,int value);
        void SaveFloat(string key,float value);
        void SaveString(string key,string value);
        void SaveBool(string key,bool value);

        int LoadInt(string key, int defaultValue = 0);
        float LoadFloat(string key, float defaultValue = 0);
        string LoadString(string key, string defaultValue = default);
        int LoadBool(string key, int defaultValue = 0);

        List<Action<ISaveUtility>> AllSaveDataAction { get; }
        void AddSaveDataAction(Action<ISaveUtility> action);
        
        void DeleteAll();
    }

    public class SaveUtility : ISaveUtility
    {
        public void SaveInt(string key,int value)
        {
            PlayerPrefs.SetInt(key,value);
        }

        public void SaveFloat(string key,float value)
        {
            PlayerPrefs.SetFloat(key,value);
        }

        public void SaveString(string key,string value)
        {
            PlayerPrefs.SetString(key,value);
        }

        public void SaveBool(string key,bool value)
        {
            PlayerPrefs.SetInt(key, value == true ? 1 : 0);
        }

        public int LoadInt(string key, int defaultValue = 0)
        {
            return PlayerPrefs.GetInt(key, defaultValue);
        }

        public float LoadFloat(string key, float defaultValue = 0)
        {
            return PlayerPrefs.GetFloat(key, defaultValue);
        }

        public string LoadString(string key, string defaultValue = default)
        {
            return PlayerPrefs.GetString(key, defaultValue);
        }

        public int LoadBool(string key, int defaultValue = 0)
        {
            return PlayerPrefs.GetInt(key, defaultValue);
        }

        public List<Action<ISaveUtility>> AllSaveDataAction { get; private set; } = new List<Action<ISaveUtility>>();

        public void AddSaveDataAction(Action<ISaveUtility> action)
        {
            AllSaveDataAction.Add(action);
        }

        public void DeleteAll()
        {
            PlayerPrefs.DeleteAll();
        }
    }
}